%% Simulate Plain VARs and Panel VAR
% by Jaromir Benes
% 
% Simulate the historical data for all three countries from a certain point
% in the past using both the VARs estimated on individual countries and the
% panel VAR.

%% Clear Workspace
%
% Clear workspace, close all figure windows, move to the top of the command
% screen, and check the IRIS version.

clear;
close all
home;
irisrequired 20130401;

%% Load Plain VARs and Panel VAR
%
% Load the historical databases for all three countries, `d`. Load the VARs
% estimated on individual countries, `vau`, `vca`, and `vno`. Load the
% estimated panel VAR, `vp`, and its output data, `dvp` from the mat file
% created in `estimate_panel`. You must run `read_data`,
% `estimate_individual_countries` and `estimate_panel` at least once before
% this m-file to create the necessary mat files.

load read_data d;
load estimate_individual_countries.mat vau vca vno;
load estimate_panel_var.mat vp;

%% Simulate Plain VARs Estimated on Individual Countries
%
% First, simulate the individual VARs in sample, from 2010:1 to 2012:2,
% using initial condition from the historical database, `d`. The function
% `simulate` returns a database with simulated data for each of the VAR
% variables. We gather these databases as entries in one super-database,
% `si`. This is not necessary in other circumstances: here, it simply makes
% the later comparion with the panel VAR results easier.

simrange = qq(2010,1) : qq(2012,2);

si = struct();
si.Au = simulate(vau,d.Au,simrange);
si.Ca = simulate(vca,d.Ca,simrange);
si.No = simulate(vno,d.No,simrange);

si %#ok<NOPTS>
si.Au
si.Ca
si.No

%% Simulate Panel VAR
%
% Simulate the same period of time using the panel VAR. When simulating a
% panel VAR, the function `simulate` needs a super-database with
% sub-databases for individual groups of data (here countries) for its
% input database, and returns a similar super-database with sub-databases
% as its output. The structure of the super-database and the sub-databases
% <?struct?> is identical to the one created above for the individual VARs.

sp = simulate(vp,d,simrange);

sp %#ok<NOPTS> %?struct?
sp.Au
sp.Ca
sp.No

%% Compare Simulations
%
% For each country, compare the simulations produced by a VAR estimated on
% that individual country and those produced by a panel VAR. Get the list
% of VAR variables, `yList` <?yList?>, and the names of groups (countries),
% `countryList` <?countryList?>, from the VAR object to annotate the
% graphs.

plotrange= simrange(1)-16 : simrange(end);

yList = get(vp,'yList'); %?yList?
countryList = get(vp,'groupNames'); %?countryList?
nCountry = length(countryList);

for iCountry = 1 : nCountry

    figure();
    country = countryList{iCountry};
    
    for iName = 1 : 4
        var = yList{iName};
        subplot(2,2,iName);
        plot(plotrange,[ ...
            si.(country).(var), ...
            sp.(country).(var), ...
            d.(country).(var)]);
        grid on;
        axis tight;
    end
    
    grfun.ftitle(['In-sample simulation: ',country]);
    grfun.bottomlegend('Individual VAR','Panel VAR','Data');

end

%% Help on IRIS Functions Used in This M-file
%
% Use either `help` to display help in the command window, or `idoc`
% to display help in an HTML browser window.
%
%    help VAR/Contents
%    help VAR/simulate
%    help VAR/get
%    help grfun/bottomlegend
%    help grfun/ftitle